﻿using System;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using SenseTimeTracker.BusinessLayer;

namespace SenseTimeTracker.TimeCards
{
	public class TimeCardDisplayItem : CommonSenseControls.CommonSenseDisplayPanelItem
	{
		private TimeCard timeCard;

		public TimeCardDisplayItem()
		{
			Height = 50;
		}

		public TimeCard TimeCard
		{
			get { return timeCard; }
			set { timeCard = value; }
		}


		#region ISenseListItem Members

		public override void OnRender(Graphics g)
		{
			using (Brush brush = new SolidBrush(Color.Black))
			using (Brush selectionBrush = new SolidBrush(Color.Gray))
			using (Font primaryFont = new Font("Helvetica Neue OTS", 9f, FontStyle.Regular))
			using (Font secondaryFont = new Font("Helvetica Neue OTS", 7f, FontStyle.Regular))
			{
				if (selected)
				{
					g.FillRectangle(selectionBrush, ClientRectangle);
				}
				SizeF taskNameTextSize = g.MeasureString(timeCard.Description, primaryFont);
				g.DrawString(timeCard.Description, primaryFont, brush, ClientRectangle.Left + 8, ClientRectangle.Top + ClientRectangle.Height * 3 / 4 - taskNameTextSize.Height / 2 - 4);
				SizeF taskNumberTextSize = g.MeasureString(timeCard.StartDateTime.ToString(), secondaryFont);
				g.DrawString(timeCard.StartDateTime.ToShortDateString(), secondaryFont, brush, ClientRectangle.Left + 8, ClientRectangle.Top + ClientRectangle.Height * 1 / 4 - taskNumberTextSize.Height / 2 + 4);
				TimeSpan duration = timeCard.FinalDateTime - timeCard.StartDateTime;
				string durationString = string.Format("{0}:{1:00}", (int)duration.TotalHours, Math.Abs(duration.Minutes));
				SizeF durationTextSize = g.MeasureString(durationString, secondaryFont);
				g.DrawString(durationString, secondaryFont, brush, ClientRectangle.Right - 8 - durationTextSize.Width, ClientRectangle.Top + ClientRectangle.Height * 1 / 4 - taskNumberTextSize.Height / 2 + 4);
			}

			base.OnRender(g);
		}

		#endregion
	}
}
